package com.zlfund.cat.zjgsmk.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.zlfund.cat.zjgsmk.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenj on 2017/9/26 0026.
 */

public class ThumbnailsView extends View {

    private Bitmap mBm;
    private int mWidth;
    private Matrix mMatrix;
    private Paint mPaint;

    public ThumbnailsView(Context context) {
        this(context, null);
    }

    public ThumbnailsView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ThumbnailsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mMatrix = new Matrix();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(getResources().getColor(R.color.cccccc));
        mPaint.setStrokeWidth(2.0f);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(),getMeasuredWidth());
    }

    public void setBitmaps(Bitmap bm){
        mBm = bm;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mBm == null){
            return;
        }
        if (mWidth == 0){
            mWidth = getWidth();
        }
        mMatrix.reset();
        int width = mBm.getWidth();
        int height = mBm.getHeight();
        int size = Math.min(width, height);
        mBm = Bitmap.createBitmap(mBm, (width - size) / 2, (height - size) / 2, size, size);
        float sx = (mWidth - 16) * 1.0f / size;
        mMatrix.postScale(sx,sx);
        mBm = Bitmap.createBitmap(mBm,0,0,size,size,mMatrix,true);
        canvas.drawBitmap(mBm,0,0,mPaint);
        canvas.drawLine(mWidth - 9,4,mWidth - 9,mWidth - 9,mPaint);
        canvas.drawLine(4,mWidth - 9,mWidth - 9,mWidth - 9,mPaint);
        canvas.drawLine(mWidth - 1,8,mWidth - 1,mWidth - 1,mPaint);
        canvas.drawLine(8,mWidth - 1,mWidth - 1,mWidth -1,mPaint);
    }
}
